package com.iwhalecloud.uncc.redis;

import com.iwhalecloud.uncc.dao.RedisClusterDao;
import com.iwhalecloud.uncc.dao.SysConfigDao;
import com.iwhalecloud.uncc.dao.impl.CtgClusterDaoImpl;
import com.iwhalecloud.uncc.dao.impl.RedisClusterDaoImpl;
import com.iwhalecloud.uncc.model.RedisConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author xu.dangling
 * @Date 2021/4/1
 */
@Slf4j
@Component
public class RedisManager {

    private static final String CTG_CACHE = "ctg";
    private static final String REDIS = "redis";

    private static RedisClusterDao redisClusterDao;

    @Value("${redis.cluster-code}")
    private String clusterCode;
    @Value("${redis.type}")
    private String redisType;
    @Autowired
    private SysConfigDao sysConfigDao;

    public void init() {
        RedisConfig redisConfig = this.sysConfigDao.getRedisConfig(clusterCode);
        if (redisConfig != null) {
            if (CTG_CACHE.equalsIgnoreCase(redisType)) {
                redisClusterDao = new CtgClusterDaoImpl();
            } else if (REDIS.equalsIgnoreCase(redisType)) {
                redisClusterDao = new RedisClusterDaoImpl();
            } else {
                log.error("WRONG TYPE OF REDIS! CHECK redis.type!");
                return;
            }
            RedisManager.getRedisClusterDao().init(redisConfig);
        }
    }

    public static RedisClusterDao getRedisClusterDao(){
        return redisClusterDao;
    }
}
